package org.app.Controller;

import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.app.Entity.DTO.UserRoleDTO;
import org.app.Entity.Group;
import org.app.Entity.GroupUser;
import org.app.Entity.comm.Result;
import org.app.Service.IGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
@CrossOrigin("*")
public class GroupController {
    @Resource
    private IGroupService groupService;

    //返回给前端该用户在该群组中的身份id，用于前端部分按钮显示
    @GetMapping("/getMyRole")
    public Result<UserRoleDTO> listMemberInfo(@RequestParam("group_id") String group_id,
                                              @RequestParam("c_id") String c_id){
        return groupService.listMemberInfo(group_id,c_id);
    }
    //返回该群组全部成员的信息，包括身份信息，用于前端展示
    @GetMapping("/getAllMembers")
    public Result<List<UserRoleDTO>> listAllMembers(@RequestParam("group_id") String group_id){
        return groupService.listAllMembers(group_id);
    }
    @GetMapping("/getGroupInfo")
    public Result<Group> getGroupInfo(@RequestParam("group_id") String group_id){
        return groupService.getGroupInfo(group_id);
    }
    //创建运动锻炼群组（教练指定训练计划）
    @PostMapping("/createExerciseGroup")
    public Result<Group> createExerciseGroup(@RequestBody Group group){
        return groupService.createExerciseGroup(group);
    }
    //创建指导锻炼群组（成员自己制定计划）
    @PostMapping("/createGuideGroup")
    public Result<Group> createGuideGroup(@RequestBody Group group){
        return groupService.createGuideGroup(group);
    }
    //群主更新成员权限
    @PutMapping("/updateAuth")
    public Result<Integer> updateAuth(@RequestBody GroupUser groupUser){
        return groupService.updateAuth(groupUser);
    }
    //群主创建群口令，用于非群成员加入群组
    @PostMapping("/createGroupToken")
    public Result<String> createGroupToken(@RequestParam("group_id") String group_id){
        return groupService.createGroupToken(group_id);
    }
    //非群成员加入群组
    @PostMapping("/join")
    public Result<Integer> joinGroup(@RequestParam("group_id") String group_id,
                                     @RequestParam("c_id") String c_id){
        return groupService.joinGroup(group_id,c_id);
    }
    //列出自己创建的全部群组（前端可以按照group_type来分别显示）
    @GetMapping("/listOwnGroup")
    public Result<List<Group>> listOwnGroup(@RequestParam("c_id") String c_id){
        return groupService.listOwnGroup(c_id);
    }
    //列出自己加入的全部群组（前端可以按照group_type来分别显示）
    @GetMapping("/listJoinGroup")
    public Result<List<Group>> listJoinGroup(@RequestParam("c_id") String c_id){
        return groupService.listJoinGroup(c_id);
    }
    //群主删除自己创建的某个群
    @DeleteMapping("/deleteGroup")
    public Result<Integer> deleteGroup(@RequestParam("group_id") String group_id){
        return groupService.deleteGroup(group_id);
    }
    //群主删除群组中的某个成员
    @DeleteMapping("/deleteMember")
    public Result<Integer> deleteMember(@RequestBody GroupUser groupUser){
        return groupService.deleteMember(groupUser);
    }
    //成员更新自己在该群组中的昵称
    @PutMapping("/updateNickName")
    public Result<Integer> updateNickName(@RequestBody GroupUser groupUser){
        return groupService.updateNickName(groupUser);
    }
    @GetMapping("/listGroupByToken")
    public Result<Group> listGroupByToken(@RequestParam("token") String token) {
        // 调用 groupService 或相关逻辑来根据 token 查询群组
        return groupService.getGroupInfoByToken(token);
    }
}
